/**
 * 详情对话框
 */
var StoreProcurementInfoDlg = {
    data: {
        customerId: "",
        customerName: "",
        orderState: "",
        peisongTime: "",
        orderOutNumber: "",
        orderType: "",
        setUnitId: "",
        setUnit: "",
        setLocation: "",
        setLocationId: "",
        setPeople: "",
        setTel: "",
        setWarehouseId: "",
        setWarehouse: "",
        getUnitId: "",
        getUnit: "",
        getLocation: "",
        getLocationId: "",
        getPeople: "",
        getTel: "",
        goodsNum: "",
        outNum: "",
        zNum: "",
        lNum: "",
        outVolume: "",
        createUserName: "",
        submitUserName: "",
        revokeUserName: "",
        createTime: "",
        submitTime: "",
        revokeTime: "",
        completeTime: "",
        notes: "",
        associationOrder: "",
        associationOrderType: "",
        goodsId: "",
        goodsName: "",
        goodsNumber: "",
        goodsPcode69: "",
        goodsSpecs: "",
        unit: "",
        rate: "",
        psNum: "",
        v1: "",
        v2: "",
        v3: "",
        v4: "",
        v5: "",
        v6: "",
        deptId: "",
        deliverId: "",
        xlbClientId: "",
        xlbRequestOrderType: "",
        xlbSetUnitId: "",
        xlbSetWarehouseId: ""
    }
};
var BasicPutgoodsInfoDlg = {
    data: {
        id:"",
        dictId:"",
        putgoodsCustomerId: "",
        putgoodsOperateName: "",
        putgoodsName: "",
        unitNumber:"",
        unitType:""
    }
};
layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/storeProcurement/detail?id=" + Feng.getUrlParam("id")+"&page=1&limit=100");
    var result = ajax.start();
    form.val('storeProcurementForm', result.data);

    $(document).ready(function(){
        // 假设页面加载时已经知道用户选择的省和市ID，可以从服务器获取这些信息或从本地存储中获取。
        var setWarehouseId = result.data.setWarehouseId; // 示例省ID，实际应用中应从服务器获取或用户输入获取。


        // 设置省的选择框的值（触发级联）并设置市的选择框的值（如果已知）
        $('#setWarehouseId').val(setWarehouseId); // 设置省的值，这会触发级联事件。
        form.render('select'); // 重新渲染以应用新的选项（可选，通常在级联事件中已处理）。
        // if (selectedCityId) { // 如果已知市ID，则设置市的选择框的值。
        //     $('#city').val(selectedCityId); // 设置市的值。通常不需要重新渲染，因为已经在省的选择触发后处理了。
        //     form.render('select'); // 如果需要在页面加载时立即看到效果，则重新渲染。通常不推荐这样做，因为这会导致不必要的重渲染。更好的做法是在API调用成功后处理。
        // } else { // 如果不知道市ID，则可以在API调用成功后设置。已在前面的AJAX调用中处理。
        //     // 这里不需要额外操作，因为已经在监听省的选择事件中处理了。
        // }
    });


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/storeProcurement/editItem", function (data) {
            Feng.success("更新成功！");
            window.location.href = Feng.ctxPath + '/storeProcurement'
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    form.on('select(getUnitId)', function (data) {
        // data.value 为当前选中的 value
        var selectedId = data.value;
        var $storeSel   = $('#setWarehouseId');
        console.log('选中的门店 id：', selectedId);

        // 这里写你要执行的逻辑，比如发送 Ajax
        $.ajax({
            url: '/basicWarehouse/getWarehouseByStoreId?page=1&limit=100',
            type: 'GET',
            data: { storeId: selectedId },
            success: function (res) {
                console.log(res);
                warehouseList = res.data
                // 处理返回结果

                var html = '<option value="">请选择仓库</option>';
                layui.each(warehouseList, function (index, item) {
                    html += '<option value="' + item.id + '">' + item.warehouseName + '</option>';
                });
                $storeSel.html(html);
                form.render('select');   // 重新渲染下拉框
            }
        });
    });

    $('#choiceGetUnit').click(function () {
        layer.open({
            type: 2,
            title: '选择收货客户',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/basicPutgoods/tochoiceBasicPutgoods_hx',
            end: function () {
                $("#getUnitId").val(BasicPutgoodsInfoDlg.data.id);
                $("#getUnit").val(BasicPutgoodsInfoDlg.data.putgoodsName);
            }
        });
    });
    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/storeProcurement'
    });

});